<?php

class HmArticle_HmLinksController extends Fw_Main
{
	public function listAction()
	{
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmArticleLinks');
		$list_link = $_model->getAll($id);
		$this->view->list_link = $list_link;
		$this->view->rowcount = count($list_link);
	}
	
	public function linksAction()
	{
		$this->listAction();
	}
	
	public function addAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$this->view->rowid = $id;
		if($this->_request->isPost()){
			$formData = $this->_request->getPost('add');
			if($formData) {
				$data = array(
							  'description'   => $this->_request->getPost('description'),
							  'url'           => $this->_request->getPost('url'),
							  'hm_article_id' => $this->_request->getPost('id')
							  );
				$_model = $this->_getModel('HmArticleLinks');
				$add_row = $_model->addRow($data);
				$this->_redirector = $this->_helper->getHelper('Redirector');
				$this->_redirector->gotoSimple('add','hm-links','hm-article',array('id' => $id));
        		return;
			}
		}
	}
	
	public function editAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmArticleLinks');
		$list_link = $_model->getRow($id);
		if($this->_request->isPost()) {
			$formData = $this->_request->getPost('edit');
			if($formData) {
				$data = array(
							  'description' => $this->_request->getPost('description'),
							  'url'         => $this->_request->getPost('url')
							  );
				$_model = $this->_getModel('HmArticleLinks');
				$update_row = $_model->updateRow($id, $data);
				$this->_redirector = $this->_helper->getHelper('Redirector');
				$this->_redirector->gotoSimple('list','hm-links','hm-article',array('id' => $id));
        		return;
			}
		}
	}
	
	public function deleteAction()
	{
		$this->validateUser();
		$id = (int)$this->_request->getParam('link_id');
		$entry_id = (int)$this->_request->getParam('id');
		$_model = $this->_getModel('HmArticleLinks');
		$delete_link = $_model->deleteRow($id);
		$this->_redirector = $this->_helper->getHelper('Redirector');
		$this->_redirector->gotoSimple('add','hm-links','hm-article',array('id' => $entry_id));
		return;
	}
}